package com.jiukuaidao.zhulin.module.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.CommContract;
import com.jiukuaidao.zhulin.module.CommPresenter;
import com.jiukuaidao.zhulin.module.CommWatcher;
import com.jiukuaidao.zhulin.module.user.contract.UpdatePasswordContract;
import com.jiukuaidao.zhulin.module.user.presenter.UpdatePasswordPresenter;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/26
 * Description :修改密码
 */
public class UpdatePasswordActivity extends BaseActivity<UpdatePasswordContract.Presenter> implements View.OnClickListener, UpdatePasswordContract.View, CommContract.CodeView {
    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.titile_text)
    TextView mTitileText;
    @BindView(R.id.et_update_code)
    EditText mEtCode;
    @BindView(R.id.tv_update_phone)
    TextView mUpdatePhone;
    @BindView(R.id.bt_code_update)
    TextView mBtCode;
    @BindView(R.id.et_update_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_update_pwd_again)
    EditText mEtPwdAgain;
    @BindView(R.id.bt_save_password)
    Button mSavePassword;
    private String mPhone;

    private CommPresenter mCommPresenter;


    @Override
    protected View getTargetView() {
        return null;
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_update_password;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        mPhone = bundle.getString("phone");
    }

    @Override
    protected void init() {
        mTitileText.setText(R.string.password_update);
        new UpdatePasswordPresenter(TaskProvider.provideTasksRepository(this), this);
        mCommPresenter = new CommPresenter(TaskProvider.provideTasksRepository(this), this);
        setOnClickListener();
        mEtPwdAgain.addTextChangedListener(new CommWatcher(mSavePassword, CommWatcher.PWDLOGINBTN));
        mUpdatePhone.setText(mPhone);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_save_password:
                mPresenter.start();
                break;
            case R.id.et_update_pwd:
                mCommPresenter.getVeriCode();
                break;
            case R.id.titile_left_imageview:
                finish();
                break;
            case R.id.bt_code_update:
                mCommPresenter.getVeriCode();
                break;
        }

    }

    public void setOnClickListener() {
        mSavePassword.setOnClickListener(this);
        mLeftImg.setOnClickListener(this);
        mBtCode.setOnClickListener(this);
    }

    @Override
    public String veriCode() {
        return mEtCode.getText().toString();
    }

    @Override
    public String password() {
        return mEtPwd.getText().toString();
    }

    @Override
    public String rePassword() {
        return mEtPwdAgain.getText().toString();
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    @Override
    public void setFinish() {
        finish();
    }

    @Override
    public String getCodeMobile() {
        return mUpdatePhone.getText().toString();
    }

    @Override
    public TextView codeBtn() {
        return mBtCode;
    }

    @Override
    public void codeToast(String msg, String errorCode) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
//        if (errorCode.equals("1010")) {
        GraphicCodeDialog codeDialog = new GraphicCodeDialog(UpdatePasswordActivity.this);
        codeDialog.showDialog();
//        }
    }
}

